﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace syc.ZhiJian
{
    public partial class 废品判定通知书管理 : System.Web.UI.Page
    {
        protected string Notice = "";
        string sql =
@"select 
年度,
月份,
产品类别ID,
ID,
品名
,isnull(sum(废品支数),0) as 废品支数,'磁粉探伤废' as 原因,供货单位,炉号
 from 磁粉探伤废接箍
";
        string sql1 =
@"select 
年度,
月份,
产品类别ID,
ID,
品名
,isnull(sum(废品支数),0) as 废品支数,'车丝废' as 原因,供货单位,炉号
 from 车丝废接箍

";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.GongHuo.DataBind();
                this.Button_Query_Click(null, null);
            }
            else
            {
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.GridView1.SelectedIndex = -1;

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        protected void PageSizeOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageSize = int.Parse(this.PageSizeOption.Text);
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {


        }
        private string GetState()
        {
            string state = "where 0=0";


            if (this.GongHuo.Text != "0")
            {
                state += " and ID=" + this.GongHuo.Text;
            }
            if (this.LeiBie.Text != "0")
            {
                state += " and 产品类别ID=" + this.LeiBie.Text;
            }
            if (this.Year.Text != "0")
            {
                state += " and 年度=" + this.Year.Text;
            }
            if (this.Month.Text != "0")
            {
                 state += " and 月份="+this.Month.Text;
            }
            //DateTime d;
            //if (DateTime.TryParse(this.Date1.Text, out d))
            //{
            //    state += " and t3.日期>='" + d.ToShortDateString() + "'";
            //}
            //if (DateTime.TryParse(this.Date2.Text, out d))
            //{
            //    state += " and t3.日期<='" + d.ToShortDateString() + "'";
            //}

            //if (state.StartsWith(" and"))
            //{
            //    state = state.Substring(4);
            //}

            return state;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string state = this.GetState();
            if (this.LeiXing.Text == "1")
            {
                this.SqlText.Text = sql + state + " group by 年度,月份,产品类别ID,ID,品名,供货单位,炉号 order by 年度,月份 desc";

            }
            if (this.LeiXing.Text == "2")
            {
                this.SqlText.Text = sql1 + state + " group by 年度,月份,产品类别ID,ID,品名,供货单位,炉号 order by 年度,月份 desc";

            }
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = this.SqlText.Text;
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.GongHuo.Text = "0";      
            this.LeiBie.Text = "0";
            this.Year.Text="0";
            this.Month.Text = "0";
        }
        
    }
}
